/**
 * Created by joustany on 17/11/22.
 */
$(function () {
    getShowView()
})
function getShowView() {
    var amount = window.localStorage.price
    var params = JSON.parse(window.localStorage.user)
    var redirect = JSON.parse(decodeURIComponent(params['redirect']));
    $.ajax({
        url: configApi('/user-coupon/index'),
        data: {
            access_token: redirect.token,
            usable: '1',
            pageSize:'20',
            amount:amount
        },
        dataType: 'json',
        type: "POST",
    }).done(function (res) {
        console.log(res)
        if (res.success){
            packetView(res.data.list)
        }else {
            layer.open({
                content: res.message
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
        }
    }).fail(function () {
        layer.open({
            content: "服务器异常,请退出重试"
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    })
}
function packetView(packet) {
    console.log(packet)
    var html = '',
        local = window.localStorage
    $.each(packet,function (index,item) {
        // console.log(item.status)
        // console.log(item)
        html +='<div class="vouchers-content" id="'+ item.id +'" onclick="">'
        html +='<div class="voucher">'
        html +='<div class="voucher—number">￥<span class="num">' + item.money+'</span></div>'
        html +=' </div>'
        html +='<div class="voucher-date">'
        html +='<span class="title">'+ item.title+'</span>'
        html +='<span class="full">'+ item.use_rule +'</span>'
        html +='<span class="unlimited">不限品类</span>'
        html +=' <span class="indate">有效期:&nbsp;<span class="date">'+ item.stime+'</span>至<span class="date">'+ item.etime+'</span></span>'
        html +='</div>'
        // if(item.status == 3){
        //     html +='<div class="icon" id="'+ item.status +'"><img class="icon_wu" src="./images/ui_icon/不可用.png"></div>'
        // }else {
            html +='<div class="icon" id="'+ item.id +'"><img class="" src="./images/ui_icon/Oval.png"></div>'
        // }
        html +='</div>'
    })
    $('.vouchers').html(html)
    console.log(local.packet)
    var packet = $.urlGet()
    //判断是否有可使用标记
    if (location.search){
        console.log($('.vouchers-content .icon'))
        var imgid = $.urlGet().id
        console.log(imgid)
        $.each($('.vouchers-content .icon'),function (i,item) {
            // console.log(this)
            // console.log(imgid)
            if (imgid == this.id){
                $(this).html('<img src="./images/ui_icon/Group.png">')
            }
        })
    }else {
        $(this).html('<img src="./images/ui_icon/Oval.png">')
    }
    //判断当前是否有不可使用标记
    console.log(packet)
    if (packet.no == '1'){
        $('.nonuse .icon img').attr('src','./images/ui_icon/Group.png')
    }else {
        $('.nonuse .icon img').attr('src','./images/ui_icon/Oval.png')
    }
}
/**
 *点击不使用红包
 * */
$(document).on('click','.nonuse',function () {
    console.log($('.nonuse .icon img').attr('src','./images/ui_icon/Group.png'))
    console.log($('.voucher-date .icon'))
    $('.vouchers-content .icon img').attr('src','')
    // //清除选择红包标记
    // window.localStorage.removeItem('packet')
    // //存储不选择标记
    // window.localStorage.no = '1'
    window.location.href = './pay.html?no=1';
})
/**
 * 点击使用当前红包
 * */
$(document).on('click','.vouchers-content',function () {
    var not = $(this).find('.icon').attr('id')
    if (not == 3){
        console.log('aaa')
    }else {
        // var packet = {}
        $('.nonuse .icon img').attr('src','./images/ui_icon/Oval.png')
        $(this).siblings().find('.icon img').attr('src','./images/ui_icon/Oval.png')
        $(this).find('.icon img').attr('src','./images/ui_icon/Group.png')
        //清除不使用红包标记
        window.localStorage.removeItem('no')
        //存储红包标记跟id
        // packet.num = $(this).find('.num').text()
        // packet.id = this.id
        // packet.no = no
        // window.localStorage.packet = JSON.stringify(packet)
        var num = $(this).find('.num').text(),
            id = this.id,
            no = 2
        window.location.href = './pay.html?num='+ num +'&id='+ id+'&no='+ no +''
    }
})
